package at.connectme.ldpath.webservice;

import at.newmedialab.lmf.ldpath.api.LDPathService;
import kiwi.core.api.triplestore.ResourceService;
import kiwi.core.model.rdf.KiWiResource;
import kiwi.core.qualifiers.transaction.Transactional;

import javax.enterprise.context.ApplicationScoped;
import javax.inject.Inject;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Response;
import java.net.URLDecoder;

/**
 * User: Thomas Kurz
 * Date: 24.01.12
 * Time: 10:45
 */
@ApplicationScoped
@Transactional
@Path("/cmf/service/ldpath")
public class LDPathWebService {

	@Inject
	LDPathService ldPathService;

	@Inject
	ResourceService resourceService;

	@GET
	public Response process(@QueryParam("uri") String uri, @QueryParam("path") String path) {
		try {
			KiWiResource resource = resourceService.createUriResource(URLDecoder.decode(uri,"utf-8"));
			return Response.ok(ldPathService.pathQuery(resource, URLDecoder.decode(path, "utf-8"), null)).build();
		} catch(Exception e) {
			e.printStackTrace();
			return Response.serverError().entity(e.getMessage()).build();
		}
	}
}
